(function(){
	angular.module("webApp").controller('FaretempCtrl',FaretempCtrl);
	FaretempCtrl.$inject=["$scope","dataService","helper","$location","$timeout"];
	function FaretempCtrl($scope,dataService,helper,$location,$timeout){
		$scope.addAction=function(){
			var fare_id=$location.search().fare_id;
			if(fare_id){
				dataService.faretempOne(fare_id).then(function(result){
					if(result.Msg){
						helper.myAlert("error:"+result.Msg);
					}else{
						
						$timeout(function(){

							async.map(result.carry_modes,function(carry_mode,callback){
								carry_mode.regionObj={};
								// carry_mode.regionObj={region_name:"",id:""};
							    var address_id=	carry_mode.region.split(",");
							     dataService.areaGet({query:{area_id:address_id[0]}}).then(function(province){
							    	 dataService.areaGet({query:{area_id:address_id[1]}}).then(function(city){
							    		dataService.areaGet({query:{area_id:address_id[2]}}).then(function(county){
							    			 carry_mode.regionObj["region_name"]=province.areas[0].area+city.areas[0].area+county.areas[0].area;
							    			carry_mode.regionObj["id"]=province.areas[0].area_id+","+city.areas[0].area_id+","+county.areas[0].area_id;
							    		});
							    	});
							    })
							     callback(null,null);
							},function(e,r){
								$scope.faretemplate=result;

							});
						})

					}
				});
			}else{
				$scope.faretemplate={};
				$scope.faretemplate.carry_modes=[];
			}
			$scope.$watch("faretemplate.carry_modes",function(newV,oldV){
				console.log("newV",newV,newV&&newV.regionObj);
				if(_.size(newV)>0&&newV[0].regionObj){
					newV[0].region=newV[0].regionObj.id;
				}
				console.log("newV",newV);
			},true);
		}
		$scope.pushcarry_mode=function(){
			var carry_mode={};
			carry_mode.region="0";
			carry_mode.first_piece=0;
			carry_mode.first_weight=0;
			carry_mode.first_bulk=0;
			carry_mode.first_amount=0;
			carry_mode.second_piece=0;
			carry_mode.second_weight=0;
			carry_mode.second_bulk=0;
			carry_mode.second_amount=0
			$scope.faretemplate.carry_modes.push(carry_mode);
		}
		$scope.faretempAdd=function(){
			// $scope.faretemplate.carry_modes="{hello}";
			if($scope.faretemplate.fare_id){
				dataService.faretempUpdate($scope.faretemplate).then(function(result){
					if(result.Msg){
						helper.myAlert("error:"+result.Msg);
					}else{
						helper.myAlert("success","#/faretemp/list");
					}
				})
			}else{
				dataService.faretempAdd($scope.faretemplate).then(function(result){
					if(result.Msg){
						helper.myAlert("error:"+result.Msg);
					}else{
						helper.myAlert("success","#/faretemp/list");
					}
				})
			}
			
		}
		$scope.listAction=function(){
			$scope.page_Size=10;
			$scope.faretempGet(1);
		}
		$scope.faretempGet=function(page_No){
			var query={page_no:page_No,page_size:$scope.page_Size}
			if($scope.fare_id){
	 			query.query={fare_id:$scope.fare_id};
	 		}
			dataService.faretempGet(query).then(function(result){
				if(result.Msg){
					helper.myAlert("error:"+result.Msg);
				}else{
					$scope.faretemplates=result.fare_temp;
				}
			});
		}
		$scope.faretemplateDelete=function(fare_id){
			// var fare_id=$location.search().fare_id;
			dataService.faretempDelete(fare_id).then(function(result){
				if(result.Msg){
					helper.myAlert("error:"+result.Msg);
				}else{
					helper.myAlert("success",null,null,function(){
						$scope.listAction();
					});
				}
			})
		}
	}
})();